#include "ReplacementPolicy.h"
#include "Cache.h"
#include "math.h"
#include <set>

ReplacementPolicy::ReplacementPolicy(){}
int ReplacementPolicy::chooseEvictee(UINT64 set){ return 0;}
void ReplacementPolicy::blockUsed(UINT64 set, int pos){}

LRU::LRU(UINT64 sets, int assoc){
    
    this->assoc = assoc;
    this->sets = sets;
    lruArray = new UINT8 [assoc*sets];
    
}

void LRU::blockUsed(UINT64 set, int pos){
    
    for(int i=0; i<assoc; i++)
        lruArray[set+i]++;
    lruArray[set+pos]=0;
   
}

int LRU::chooseEvictee(UINT64 set){
    
    int pos=0;
    
    //cycle through all candidates and choose greatest value
    for(int i = 0; i<assoc; i++){
        if(lruArray[set+i]>lruArray[set+pos]) 
            pos = i;
        lruArray[set+i]++; //increment all
    }
    
    lruArray[set+pos]=0;//reset this position since it's being replaced
    
    return pos;
   
}
RoundRobin::RoundRobin(UINT64 sets,int assoc){
    
    this->assoc = assoc;
    shift = log2(assoc);
    assocMask = UINT16(assoc) - 1;
    rrArray = new UINT16[sets];
    
    
    for(unsigned int i=0; i<sets; i++)
        rrArray[i]=0;
    
}

int RoundRobin::chooseEvictee(UINT64 set){
    
    set = set >> shift;
    
    rrArray[set] = (rrArray[set]+1) & assocMask; //increment round robin counter
    return rrArray[set];

}



